<?php
/**
 * 权限控制 ver 1.0-2011.9.16
 */
class Helper_Functions
{
	/**
	 * 
	 * 默认模块名称
	 * @var string
	 */
	static $default_modules = 'default';
	/**
	 * 
	 * 默认命名空间
	 * @var string
	 */
	static $default_namespace = 'default';
	/**
	 * 
	 * 模块路径
	 * @var string
	 */
	static $modules_dir = '';
	/**
	 * 
	 * 控制器路径
	 * @var string
	 */
	static $controller_dir = '';
	static $interface = NULL;
	/**
	 * 
	 * 构造函数
	 */
	function __construct()
	{
		$app_config = Q::ini('app_config');
		self::$controller_dir = $app_config ['APP_DIR'].'/controller/';
		self::$modules_dir = $app_config ['MODULE_DIR'];
	}
	/**
	 * 
	 * 单件入口
	 */
	static function getInterface() {
		if (self::$interface == null)
		{
			self::$interface = new self();
		}
		return self::$interface;
	}
	/**
	 * 
	 * 获取所有模块返回数组
	 * @return array
	 */
	function get_modules()
	{
		$modules  = array(self::$default_modules);
		if (is_dir(self::$modules_dir)) {
			$handle = opendir(self::$modules_dir);
			while (false !== ($file = readdir($handle)))
			{
				if ($file != '.' && $file != '..')
				{
					if (filetype(self::$modules_dir.'/'.$file) == 'dir') $modules[] = $file;
				}
			}
			closedir($handle);
		}
		return $modules;
	}
	/**
	 * 
	 * 获取所有命名空间
	 * @return array
	 */
	function get_namespaces()
	{
		$modules = $this->get_modules();
		$namespaces = array(self::$default_modules => array(self::$default_namespace));
		foreach ($modules as $module)
		{
			$module_controller_dir = self::$controller_dir;
			if (is_dir(self::$modules_dir.'/'.$module))
			{
				$module_controller_dir = self::$modules_dir.'/'.$module.'/controller/';
			}
			// get folder name
			if (is_dir($module_controller_dir))
			{
				$handle = opendir($module_controller_dir);
				while (false !== ($file = readdir($handle)))
				{
					if ($file != '.' && $file != '..')
					{
						if (filetype($module_controller_dir.'/'.$file) == 'dir'){
							$namespaces[$module][] = $file;
						}
					}
				}
				closedir($handle);
			}
		}
		return $namespaces;
	}
	/**
	 * 
	 * 获取所有控制器
	 * @return array
	 */
	function get_controllers()
	{
		$namespaces = $this->get_namespaces();
		$controllers = array();
		foreach ($namespaces as $module=>$namespace)
		{
			$controller_dir = self::$modules_dir.'/'.$module.'/controller/';
			if ($module == self::$default_modules)
			{
				$controller_dir = self::$controller_dir;
			}
			foreach ($namespace as $item)
			{
				$controller_dir_tmp = $controller_dir;
				if ($item != self::$default_namespace)
				{
					$controller_dir_tmp .= $item;
				}
				if (is_dir($controller_dir_tmp))
				{
					$handle = opendir($controller_dir_tmp);
					while (false !== ($file = readdir($handle)))
					{
						if ($file != '.' && $file != '..')
						{
							if (filetype($controller_dir_tmp.'/'.$file) == 'file'){
								if (strpos($file,'_controller.php'))
								{
									$controllers[$module][$item][] = substr($file, 0,strpos($file,'_controller.php'));
								}
							}
						}
					}
				}
			}
		}
		return $controllers;
	}
	/**
	 * 
	 * 获取获取所有方法
	 * @return array
	 */
	function get_actions()
	{
		$controllers = $this->get_controllers();
		$actions = array();
		foreach ($controllers as $module=>$namespace)
		{
			$controller_dir = self::$modules_dir.'/'.$module.'/controller/';
			if ($module == self::$default_modules)
			{
				$controller_dir = self::$controller_dir;
			}
			foreach ($namespace as $ns=>$controller)
			{
				$controller_dir_tmp = $controller_dir;
				if ($ns != self::$default_namespace)
				{
					$controller_dir_tmp .= $ns.'/';
				}
				foreach ($controller as $item)
				{
					$file_name = $controller_dir_tmp.$item.'_controller.php';
					$file_content = php_strip_whitespace($file_name);
			    	preg_match_all('/function action[a-zA-z0-9]*/', $file_content, $t_action);
			    	foreach ($t_action[0] as $t_act)
			    	{
			    		$actions[$module][$ns][$item][] = strtolower(substr($t_act, strpos($t_act, 'action')+strlen('action')));
			    	}
				}
			}
		}
		return $actions;
	}
}